|
|
|
|
|
|
|
Now alter the Employees table. You need to do two things: |
|
|
|
|
|
|
|
|
· Remove the DeptName column from the table. |
|
|
|
|
|
|
|
|
· Add a FOREIGN KEY constraint to enforce referential integrity on the Employees.DeptID field. |
|
|
|
|
|
|
|
|
Listing 16.5 contains the SQL DDL statements to create the modified Employees table. |
|
|
|
|
|
|
|
|
Listing 16.5. Creating the new Employees table. |
|
|
|
|
|
|
|
|
CREATE TABLE Employees
(EmpID TEXT(5),
EmpName TEXT(30),
DeptID TEXT(5),
CONSTRAINT PKEmpID PRIMARY KEY (EmpID),
CONSTRAINT FKEmpDept FOREIGN KEY (DeptID)
REFERENCES Departments(DeptID)) |
|
|
|
|
|
|
|
|
Now you need to modify the EmpSkills table to add the referential integrity check on the EmpSkills.SkillDeptID field. The new SQL DDL should look like Listing 16.6. |
|
|
|
|
|
|
|
|
Listing 16.6. Creating the new EmpSkills table. |
|
|
|
|
|
|
|
|
CREATE TABLE EmpSkills2
(EmpID TEXT(5),
SkillCode TEXT(5),
SkillDeptID TEXT(5),
SkillLevel INTEGER,
CONSTRAINT PKEmpSkill2 PRIMARY KEY (SkillCode,EmpID),
CONSTRAINT FKSkillMast FOREIGNKEY (SkillCode)
REFERENCES SkillMaster(SkillCode),
CONSTRAINT FKSkillDept FOREIGN KEY (SkillDeptID)
REFERENCES Departments(DeptID)); |
|
|
|
|
|
|
|
|
The database NORMDAT4.MDB contains a complete set of tables that conform to the third rule of data normalization. Use Visdata to load NORMDAT4.MDB and review the data tables. Attempt to add some data that does not follow the integrity rules. Try deleting records. This shows you how Visual Basic issues database error messages when you try to save a record that breaks the referential integrity rules. |
|
|
|
|
|
|
|
|
The first three rules of data normalization involve the elimination of repeating, redundant, or unrelated data fields. The last two rules involve isolating multiple relationships to improve overall database integrity. The first three rules are usually all that you need to produce well-designed databases. However, there are times when additional normalization can improve the quality of your database design. In the next two sections, you learn rules 4 and 5 of data normalization. |
|
|
|
|
|